package com.yutel.phone.xplayer;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.view.KeyEvent;

import com.yutel.phone.xplayer.ui.AirplayNotifier;

public class XPlayerConfig extends PreferenceActivity implements
		OnSharedPreferenceChangeListener {

	private AirplayNotifier mNotifier;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mNotifier = AirplayNotifier.getInstance(this);
		mNotifier.updateNotification();
		SharedPreferences sharedPreferences = getPreferenceManager()
				.getSharedPreferences();
		sharedPreferences.registerOnSharedPreferenceChangeListener(this);
		addPreferencesFromResource(R.xml.preferences);
	}

	@Override
	protected void onDestroy() {
		getPreferenceManager().getSharedPreferences()
				.unregisterOnSharedPreferenceChangeListener(this);
		super.onDestroy();
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		if (key.equals("received")) {
			mNotifier.updateNotification();
		} else if(key.equals("startserver")) {
			mNotifier.setChanged(true);
			mNotifier.updateNotification();
		}
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
			System.exit(0);
			return true;
		}
		return false;
	}
}